<?php
/*
 * Author: Quan Van Sinh
 * Email: sinhvnb@vietnambiz.com
 * Update: NghiaTruong
 * Email: nghiatruong@vietnambiz.com
 * Date: 16-3-2011
 */
checkLoginHeader();
checkPermission();
$user = new user();
$user->userAction();

?>
<?php include_once $config ['admin_theme_path'].$config ['admin_theme'].'/header.php'; ?>


<script type="text/javascript">
    function validateAddUserForm(thisform){
        {
            with (thisform)
              {
              if (validate_required(username,"Username must be filled out!")==false)
                {username.focus();return false;}
              if (validate_required(email,"Email must be filled out!")==false)
                {email.focus();return false;}
              if (validate_email(email,"Not a valid e-mail address!")==false)
                {email.focus();return false;}
              if (validate_required(password,"Password must be filled out!")==false)
                {password.focus();return false;}
              }
            }
        }
    function validateEditUserForm(thisform){
        {
            with (thisform)
              {
              if (validate_required(edUsername,"Username must be filled out!")==false)
                {edUsername.focus();return false;}
              if (validate_required(edEmail,"Email must be filled out!")==false)
                {edEmail.focus();return false;}
              if (validate_email(edEmail,"Not a valid e-mail address!")==false)
                {edEmail.focus();return false;}
              if (validate_required(edPassword,"Password must be filled out!")==false)
                {edPassword.focus();return false;}
              }
            }
        }

        
    function addUser(){
        document.getElementById('userAction').style.display = '';
        document.getElementById('formAddUser').style.display = '';
        document.getElementById('formEditUser').style.display = 'none';
    }
    function editUser(id){
        document.getElementById('userAction').style.display = '';
        document.getElementById('formAddUser').style.display = 'none';
        document.getElementById('formEditUser').style.display = '';
        $.ajax({
            url: '/<?php echo $config ['admin_section']; ?>/user',
            type: 'POST',
            dataType: 'json',
            data: "id="+id+"&userActionType=editUser",
            timeout: 1000,
            error: function(){
                alert('Error loading user infomation');
            },
            success: function(data){
                $("#edId").val(data.id);
                $("#edFullname").val(data.fullname);
                $("#edUsername").val(data.username);
                $("#edEmail").val(data.email);
                $("#edPassword").val(data.password);
                if(data.active==1){
                    $("#edActive").attr("checked","true");
                }else if(data.active==0){
                    $("#edActive").attr("checked","");
                }
                $("#userActionType").val('updateUser');
                
                
            }
        });
    }

    

    function deleteUser(id,txtAlert){
        if (confirm("Do you want to delete user "+txtAlert+"?")){
            $.ajax({
                url: '/<?php echo $config ['admin_section']; ?>/user',
                type: 'POST',
                data: "id="+id+"&userActionType=deleteUser",
                error: function(){
                    alert('Error loading user infomation');
                },
                success: function(){
                    window.location = "/<?php echo $config ['admin_section']; ?>/user";
                }
            });
        }
    }

    $(document).ready(function(){
        document.getElementById('userAction').style.display = 'none';
        setTimeout("closeResultMessages()",5000);
    });
    function closeResultMessages(){
        document.getElementById('rsMessages').style.display = 'none';
    }
    function closeHtml(id){
        document.getElementById(id).style.display = 'none';
    }
    
</script>

<div id="wrapper">
            <div id="content" style="width: 98%;">

                <div id="userAction">
                <div id="box">
                      <div id="rightnow">
                          <h3 class="reallynow">
                            <span>User Actions</span>
                            <a href="javascript: closeHtml('userAction');">Close</a><br />
                          </h3>
                      </div>
                      <div id="formAddUser">
                          <form method="post" action="/<?php echo $config ['admin_section']; ?>/user" id="form" name="formAddUser" onsubmit="return validateAddUserForm(this);" style="padding-top: 10px">
                          <fieldset id="fsAddUser">
                            <legend>Add user</legend>
                            <label for="fullname">User name : </label>
                            <input style="width: 200px" type="text" tabindex="1" id="username" name="username"/>
                            <input type="hidden" id="userActionType" name="userActionType" value="addUser"/>
                            <br/>
                            <label for="fullname">Full name : </label>
                            <input style="width: 200px" type="text" tabindex="2" id="fullname" name="fullname"/>
                            <br/>
                            <label for="email">Email : </label>
                            <input style="width: 200px" type="text" tabindex="3" id="email" name="email"/>
                            <br/>
                            <label for="password">Password : </label>
                            <input style="width: 200px" type="password" tabindex="4" id="password" name="password"/>
                            <br/>
                            <label for="active">Active user : </label>
                            <input type="checkbox" tabindex="6" value="1" id="active" name="active"/>
                            <br/>

                          </fieldset>

                      <div align="center">
                      <input type="submit" value="Create user" name="addUserSubmit" id="addUserSubmit"/>
                      <input type="reset" id="AddUserReset"/>
                      </div>
                    </form>
                    </div>

                    <div id="formEditUser">
                        <form method="post" action="/<?php echo $config ['admin_section']; ?>/user" id="form" onsubmit="javascript: return validateEditUserForm(this);" name="formEditUser">
                          <fieldset id="fsEditUser">
                            <legend>Edit user</legend>
                            <label for="fullname">User name : </label>
                            <input style="width: 200px" type="text" tabindex="1" id="edUsername" name="edUsername"/>
                            <input type="hidden" id="edId" name="edId"/>
                            <input type="hidden" id="userActionType" name="userActionType" value="updateUser"/>
                            <br/>
                            <label for="fullname">Full name : </label>
                            <input style="width: 200px" type="text" tabindex="2" id="edFullname" name="edFullname"/>
                            <br/>
                            <label for="email">Email : </label>
                            <input style="width: 200px" type="text" tabindex="3" id="edEmail" name="edEmail"/>
                            <br/>
                            <label for="password">Password : </label>
                            <input style="width: 200px" type="password" tabindex="4" id="edPassword" name="edPassword"/>
                            <br/>
                            <label for="active">Active user : </label>
                            <input type="checkbox" tabindex="6" value="1" id="edActive" name="edActive"/>
                            <br/>

                          </fieldset>

                      <div align="center">
                          <input type="submit" value="Update user infomation" name="editUserSubmit" id="editUserSubmit"/>
                      </div>
                    </form>
                    </div>
                </div>
                <div style="height:10px"></div>
                </div>

                
                <div id="rightnow">
                    <h3 class="reallynow">
                        <span>Users</span>
                        <span style="float: right"><a class="add" href="javascript: addUser();">Add user</a></span>
                        <br />
                    </h3>
                    <div class="youhave">
                        <div id="rsMessages" style="text-align: center; color: red; font-weight: bolder"><? if(isset($_SESSION['rsMessages'])){echo $_SESSION['rsMessages']; $_SESSION['rsMessages']='';} ?></div>
                        <table width="100%">

                            <tbody>
                                <tr>
                                    <td width="40px">&nbsp;</td>
                                    <td><b>Full Name</b></td>
                                    <td><b>User Name</b></td>
                                    <td><b>Email</b></td>
                                    <td width="60px"><b>Actived</b></td>
                                    <td width="60px"><b>Action</b></td>
                                </tr>
                                <?php
                                    $rsAllUser = $user->getAllUser();
                                    $i=0;
                                    while($rs=$db->fetch_array($rsAllUser)){
                                        $i++;
                                ?>
                                <tr>
                                    <td class="a-center"><? echo $i;?></td>
                                    <td><? echo $rs['fullname'];?></td>
                                    <td><span style="color: red;"><? echo $rs['username'];?></span></td>
                                    <td><? echo $rs['email'];?></td>
                                    <td><?
                                        if($rs['active']==1){
                                            echo 'Yes';
                                        }else{
                                            echo 'No';
                                        }
                                    ?></td>
                                    <td><a href="#"><img height="16" width="16" title="Show profile" src="<?php echo $config ['admin_images_url'];?>/icons/user.png"/></a><a href="javascript: editUser('<?php echo $rs['id'];?>');"><img height="16" width="16" title="Edit user" src="<? echo $config ['admin_images_url'];?>/icons/user_edit.png"/></a><a href="javascript: deleteUser('<?php echo $rs['id'];?>','<?php echo $rs['username'];?>');"><img height="16" width="16" title="Delete user" src="<?php echo $config ['admin_images_url'];?>/icons/user_delete.png"/></a></td>
                                </tr>
                                <?php }?>
                                </tbody>
                        </table>
                    </div>
                  </div>                  
            </div>


      </div>
<?php include_once $config ['admin_theme_path'].$config ['admin_theme'].'/footer.php'; ?>